package com.android.bookgarden.ui;

import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.bookgarden.R;
import com.android.bookgarden.adapter.CollectionRecordsAdapter;
import com.android.bookgarden.apis.Apis;
import com.android.bookgarden.base.BaseActivity;
import com.android.bookgarden.bean.DataBean;
import com.android.bookgarden.bean.LeanBookBean;
import com.android.bookgarden.bean.RecordsBean;
import com.android.bookgarden.constract.RealtConstract;
import com.android.bookgarden.network.HttpType;
import com.android.bookgarden.persenter.BasePersenter;
import com.android.bookgarden.utli.JsonUitl;
import com.android.bookgarden.utli.LogUtil;
import com.android.bookgarden.utli.Utlis;
import com.android.bookgarden.views.LoadingDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author XLC
 * @description 交易明细
 * @time 2018-11-29
 */
public class TransactionDetailsUI extends BaseActivity implements View.OnClickListener,RealtConstract.View {
    @BindView(R.id.close)
    ImageView close;
    @BindView(R.id.title_text)
    TextView titleText;
    @BindView(R.id.ritgh_icon)
    ImageView ritghIcon;
    @BindView(R.id.ritgh_text)
    TextView ritghText;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;

    private List<RecordsBean> list;
    private CollectionRecordsAdapter adapter;

    private RealtConstract.Presenter presenter;
    private int pageIndex = 1;
    private int pageSize = 20;
    private boolean flag = false;
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close:
                finish();
                break;
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.transaction_details_ui;
    }

    @Override
    protected void initView() {
        titleText.setText("交易明细");
        close.setOnClickListener(this);
        presenter = new BasePersenter(this,this);
        list = new ArrayList<>();
//        list.add(new RecordsBean("***的王二", "2018年8月11号", "8827.00"));
//        list.add(new RecordsBean("***的王二", "2018年8月11号", "887.00"));
//        list.add(new RecordsBean("***的王二", "2018年8月11号", "887.00"));
//        list.add(new RecordsBean("***的王二", "2018年8月11号", "8817.00"));
//        list.add(new RecordsBean("***的王二", "2018年8月16号", "8687.00"));
//        list.add(new RecordsBean("***的王二", "2018年8月16号", "887.00"));
//        list.add(new RecordsBean("***的王二", "2018年8月17号", "8827.00"));
//        list.add(new RecordsBean("***的王二", "2018年8月17号", "887.00"));
        adapter = new CollectionRecordsAdapter(list, this);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // 设置是否开始刷新,true为刷新，false为停止刷新
                swipeRefreshLayout.setRefreshing(true);
                        pageIndex= 1;
                        list.clear();
                        adapter.notifyDataSetChanged();
                        getDataList();

            }
        });

        getDataList();
    }

    @Override
    public void onRequestPermissions(String str, boolean z) {

    }

    private void getDataList() {
        LoadingDialog.showMassge(this,"加载中...");
        presenter.loadDataHeader(null, "", Apis.GET_TRANSACTION + pageIndex + "/" + pageSize , 1001, Utlis.getHeadler(this), HttpType.GET);
    }


    @Override
    public void returnData(String data, int urlCode) {
        LogUtil.e("返回数据", data + "");
        if (TextUtils.isEmpty(data)) {
            return;
        }
        LoadingDialog.hideProgress();
        handler.obtainMessage(urlCode, data).sendToTarget();
    }

    @Override
    public void LoadError(String errorMsg, int urlCode) {
        showToast("请求失败");
        LoadingDialog.hideProgress();
    }



    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String data = (String) msg.obj;
            switch (msg.what) {


                case 1001:
                    swipeRefreshLayout.setRefreshing(false);
                    DataBean bean = Utlis.getDataBean(data);
                    if (null == bean) {
                        showToast("获取列表失败");
                        return;
                    }
                    if (bean.getCode() == 200) {
                        if (bean.getCode() == 200) {
                            List<RecordsBean> listD = JsonUitl.stringToList(bean.getData(), RecordsBean.class);
                            if (null == listD || listD.size() <= 0) {
                                adapter.setFooterVisible(View.GONE);
                                flag = false;
                                return;
                            }
                            list.addAll(listD);
                            adapter.notifyDataSetChanged();
                            if (listD.size()<pageSize){
                                adapter.setFooterVisible(View.GONE);
                                flag = false;
                            }else {
                                flag = true;
                            }

                        } else {
                            showToast(bean.getMessage() + "");
                        }


                        break;

                    } else {
                        showToast(bean.getMessage() + "");
                    }


                    break;



            }
        }
    };
}
